package telex;
/*
 * QueryString.java
 *
 * Copyright (c) 2007-2008 Colin Bayer, Douglas Downey, Oren Etzioni,
 *   University of Washington Computer Science and Engineering
 * 
 * See the file "COPYING" in the root directory of this distribution for
 * full licensing information.
 */

import java.util.Arrays;

/* 
 * ridiculous String array wrapper class because Sun couldn't be bothered to
 * make Arrays.hashCode() actually useful. 
 */
public class QueryString {
	public QueryString() {
		mWords = new String[0];
	}
	
	public QueryString(String[] aWords) {
		if (aWords == null) throw new IllegalArgumentException();
		
		mWords = aWords.clone();
	}
	
	public String[] get() { return mWords.clone(); }
	public String getWord(int aIdx) { 
		if (aIdx < 0 || aIdx >= mWords.length) throw new IllegalArgumentException();
		return mWords[aIdx];
	}
	
	public String getFlattened(String aDelim) {
		String s = "";
		
		for (int i = 0; i < mWords.length; i++) {
			s += mWords[i];
			if (i + 1 != mWords.length) s += aDelim;
		}
		
		return s;
	}
	public String getFlattened() { return getFlattened(" "); }
	
	public int length() { return mWords.length; }
	
	public int hashCode() { return Arrays.hashCode(mWords); }
	public boolean equals(Object aOther) {
		if (!(aOther instanceof QueryString)) return false;
		
		return (compareTo((QueryString)aOther) == 0);
	}
	public int compareTo(QueryString aOther) {
		if (aOther == null) return 1;
		else if (mWords.length < aOther.mWords.length) return -1;
		else if (mWords.length > aOther.mWords.length) return 1;
		else {
			for (int i = 0; i < mWords.length; i++) {
				int c = mWords[i].compareTo(aOther.mWords[i]);
				
				if (c != 0) return c;
			}
			
			// all words compared equal; the two queries are equal.
			return 0;
		}
	}
	
	private String[] mWords;
}
